﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BCGC.BLL.Helper;
using BCGC.BLL.BusinessObject.Resource;
using log4net;
using Jalal.Library.Utility;
using System.Collections.Generic;

public partial class Admin_Resource_Wallpaper_AddMoreSize : AdminBasePage
{
    private int Id
    {
        get
        {
            if (ViewState["Id"] == null)
                ViewState["Id"] = 0;
            return (int)ViewState["Id"];
        }
        set { ViewState["Id"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int id = Id = 0;
            if (!string.IsNullOrEmpty(Request.QueryString["id"]) && int.TryParse(Request.QueryString["id"], out id))
            {
                Id = id;
                LoadData();
            }
            else
                Response.Redirect(CustomSiteMap.Wallpapers);
        }
    }

    private void LoadData()
    {
        Wallpaper currentWallpaper = Wallpaper.GetWallpaperById(Id);
        imgWallpaperThumb.ImageUrl = currentWallpaper.ThumbnailFileUrl;
    }

    protected void Size_Changd(object sender, EventArgs e)
    {
        if (ddlSize.SelectedValue == "Other")
            plhCustomSize.Visible = true;
        else
            plhCustomSize.Visible = false;
    }

    protected void Create_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            try
            {
                if (!WallpaperSize.AllreadySizeExists(ddlSize.SelectedValue))
                {
                    Wallpaper currentWallpaper = Wallpaper.GetWallpaperById(Id);
                    int width = 0;
                    int height = 0;
                    bool widhtHeightFound = false;
                    if (ddlSize.SelectedValue == "Other")
                    {
                        if (int.TryParse(tbxWidth.Text, out width) && int.TryParse(tbxHeight.Text, out height))
                            widhtHeightFound = true;
                    }
                    else if (ddlSize.SelectedValue != "None")
                    {
                        string[] sizeParts = ddlSize.SelectedValue.Split(new char[] { 'x' });
                        if (sizeParts.Length >= 2)
                        {
                            if (int.TryParse(sizeParts[0], out width) && int.TryParse(sizeParts[1], out height))
                                widhtHeightFound = true;
                        }
                    }

                    bool uploadSuccess = false;
                    if (widhtHeightFound)
                    {
                        List<System.Drawing.Size> sizes = new List<System.Drawing.Size>();
                        sizes.Add(new System.Drawing.Size(width, height));
                        uccImageUploader.Sizes = sizes;
                        List<string> fileUrl = uccImageUploader.UploadImage();
                        if (fileUrl.Count > 0)
                        {
                            if (WallpaperSize.Create(Id, ddlSize.SelectedValue, fileUrl[0]))
                            {
                                uccResponse.SetSuccessMessage("New size wallpaper uploaded");
                                uploadSuccess = true;
                            }
                        }
                    }

                    if (!uploadSuccess)
                        uccResponse.SetFailMessage("Failed to upload new size wallpaper");
                }
                else
                    uccResponse.SetFailMessage("Selected size already exists");
            }
            catch (Exception ex)
            {
                // Write the log into handled exception list
                ILog log = LogHelper.GetHandledFileLogger("AddMoreSize");
                if (log.IsErrorEnabled)
                    log.Error("Failed to add size image to wallpaper", ex);

                uccResponse.SetFailMessage("There was a problem in uploading size wallpaper");
            }
        }
    }
}
